class DiscussionBoardController < ApplicationController
  after_filter :post_log, :except=>[:index, :forum_list]
  
  def index
    #@user = User.find(:first, :conditions => { :login => 'songrit'})
    #if @user!=nil
    #  session[:user] = @user
    #end
    
    #if session[:user]!=nil
      redirect_to :action => "forum_list"
    #else
    #  render :text => "NO"
    #end
  end
  
  def forum_list
    @forums = Forum.find(:all, :conditions => {:status_flag => 'Y'})
    render :layout => 'main'
  end
  
  def view_forum
    @forum = Forum.find(params[:id])
#    @topic_pages, @topics = paginate :topics, :conditions => {:forum_id => params[:id], :status_flag => 'Y'}, :per_page => 10
    @topics = Topic.paginate :page=>params[:page],
      :order=>'create_on DESC',
      :conditions => {:forum_id => params[:id], :status_flag => 'Y'},
      :per_page => 20
    render :layout => 'main'
  end
  
  def new_topic
    @forum = Forum.find(params[:id])
    render :layout => 'main'
  end
  
  def test
    render :text => DlearnUtility.get_operator_email
  end
  
  def create_topic
    @topic = Topic.new(params[:topic])
    @topic.create_by = begin current_user.login rescue nil end
    @topic.create_on = Time.now
    @topic.status_flag = "N"
    @topic.save
    #send_email('postmaster@velocall.com', 'post master', DlearnUtility.get_operator_email, '', 'Mail from Discussion Board', 'You have a new topic to consideration')
    #Notifier.deliver_topic_send(DlearnUtility.get_operator_email, topic)
    #http("http://email.velocall.com/main/send_mail?to="+DlearnUtility.get_operator_email+"&subject="+(DlearnUtility.get_topic_subject.gsub("\r","").gsub("\n",""))+"&body="+(DlearnUtility.get_topic_body.gsub(" ","%20")))
    #http("http://email.velocall.com/main/send_mail?to=comnas@gmail.com&subject=test%20maildloc&body=hellodloc")
    @message = DlearnUtility.get_topic_body
    mail_body = render_to_string :action => "create_topic", :template => "notifier/topic_send", :layout => false
    #render :text => mail_body
    Mail.sendmail(DlearnUtility.get_system_email, DlearnUtility.get_operator_email, DlearnUtility.get_topic_subject, mail_body)
    flash[:notice] = 'ระบบได้รับข้อความกระทู้ของท่านแล้ว กระทู้ของท่านจะยังไม่แสดงในขณะนี้จนกว่าเจ้าหน้าที่ระบบจะดำเนินการพิจารณาต่อไป'
    redirect_to :action => 'view_forum', :id => params[:topic][:forum_id]
  end
  
  def view_topic
    @topic = Topic.find(params[:id])
    @topic.read_count = @topic.read_count + 1
    @topic.save
    @forum = Forum.find(@topic.forum_id)
#    @answer_pages, @answers = paginate :answers, :conditions => {:topic_id => params[:id], :status_flag => 'Y'}, :per_page => 10
    @answers = Answer.paginate :page=>params[:page], :conditions => {:topic_id => params[:id], :status_flag => 'Y'}, :per_page => 10
    render :layout => 'main'
  end
  
  def send_email(from, from_alias, to, to_alias, subject, message)
	msg = <<END_OF_MESSAGE
From: #{from_alias} <#{from}>
To: #{to_alias} <#{to}>
Subject: #{subject}
	
#{message}
END_OF_MESSAGE
        Net::SMTP.start('203.147.57.69', 26, 'localhost.localdomain', 'songrit', 'velocall123', :login) do |smtp|
	#Net::SMTP.start('203.147.62.36', 25, 'localhost.localdomain', 'velomail', 'forvelouser', :login) do |smtp|
	#Net::SMTP.start('203.147.62.36') do |smtp|
		smtp.send_message msg, from, to
	end
  end
end

